import { Body, Controller, Get, Post, Res } from '@nestjs/common';
import { Response } from 'express';
import * as fs from 'fs';
import * as path from 'path';
import { InstallService } from './install.service';

@Controller('install')
export class InstallController {
  @Get()
  getInstallPage(@Res() res: Response) {
    const installPath = path.join(
      process.cwd(),
      'public/install-page/install.html',
    );
    console.log(installPath);
    if (fs.existsSync(installPath)) {
      return res.sendFile(installPath);
    }
    return res.send('<h1>安装页面</h1><p>欢迎使用likeadmin安装向导</p>');
  }

  @Get('environment')
  checkEnvironment(@Res() res: Response) {
    const installService = new InstallService();
    const environment = installService.checkEnvironment();
    return res.json(environment);
  }

  @Post('create-table')
  async createTable(
    @Res() res: Response,
    @Body() body: { admin: { username: string; password: string } },
  ) {
    const installService = new InstallService();
    const result = await installService.createTable(body.admin);
    return res.json(result);
  }
}
